
class myDoubleSlider {
  
  float sliderX1, sliderY1;
  float sliderX2, sliderY2;

  float sliderMin;
  float sliderMax;
  float sliderMajorInterval;
  float sliderMinorInterval;

  float sliderBar1X1, sliderBar1X2;
  float sliderBar2X1, sliderBar2X2;
  float sliderBarSize;
  int slider1FillColor, slider2FillColor;
  
  //float sliderBar1LabelX1, sliderBar1LabelX2;
  //float sliderBar2LabelX1, sliderBar2LabelX2;
 
  boolean zoomOn;
  boolean drawLabels;

  myDoubleSlider(float sliderX1, float sliderY1, float sliderX2, float sliderY2, float sliderMin, float sliderMax, float sliderMajorInterval, float sliderMinorInterval){
    
    this.sliderX1 = sliderX1;
    this.sliderY1 = sliderY1;
    this.sliderX2 = sliderX2;
    this.sliderY2 = sliderY2;
    this.sliderMin = sliderMin;
    this.sliderMax = sliderMax;
    this.sliderMajorInterval = sliderMajorInterval;
    this.sliderMinorInterval = sliderMinorInterval;
    
    drawLabels = true;
    
    sliderBarSize = (sliderX2-sliderX1)/100 ; 
    //init slider position to ends
    sliderBar1X1 = sliderX1-sliderBarSize/2;//CHANGE sliderX1;
    sliderBar1X2 = sliderBar1X1+sliderBarSize;
    sliderBar2X1 = sliderX2 - sliderBarSize/2;//change - sliderBarSize
    sliderBar2X2 = sliderX2 + sliderBarSize/2; //CHANGE sliderX2
    
    slider1FillColor = slider2FillColor = 200;
    
    zoomOn = false;
  }
  
  //NOTE: did not implement change fully here
  myDoubleSlider(float sliderX1, float sliderY1, float sliderX2, float sliderY2){
    this.sliderX1 = sliderX1;
    this.sliderY1 = sliderY1;
    this.sliderX2 = sliderX2;
    this.sliderY2 = sliderY2;
    
    drawLabels = false;
    
    sliderBarSize = (sliderX2-sliderX1)/100 ; 
    //init slider position to ends
    sliderBar1X1 = sliderX1;
    sliderBar1X2 = sliderBar1X1+sliderBarSize;
    sliderBar2X1 = sliderX2 - sliderBarSize;
    sliderBar2X2 = sliderX2; 
    
    slider1FillColor = slider2FillColor = 200;
    
    zoomOn = false;
  }
    
  void drawSlider(){ 
    strokeWeight(1);
    
    //left rectangle
    rectMode(CORNERS);
    noStroke();
    fill(67, 59, 73, 125);
    rect(sliderX1, sliderY1, sliderBar1X1, sliderY2);
    
    //right rectangle
    rect(sliderBar2X2, sliderY1, sliderX2, sliderY2);
   
    //central rectangle
    fill(100, 100, 100, 125);
    rect(sliderBar1X1, sliderY1, sliderBar2X2, sliderY2);
    
    //draw slection bar 1
    fill(slider1FillColor,slider1FillColor, slider1FillColor, 125);
    rect(sliderBar1X1, sliderY1, sliderBar1X2, sliderY2);
    //stroke(255, 255, 255, 125);
    //line( (sliderBar1X1+sliderBar1X2)/2, sliderY1, (sliderBar1X1+sliderBar1X2)/2, sliderY2);
    rect( sliderBar1X1-15, sliderY2, sliderBar1X2+15, sliderY2+15);
    fill(255);
    textSize(9);
    textAlign(CENTER);
    float midpoint = (sliderBar1X1 + sliderBar1X2)/2;
    text( getMonth( midpoint ) + "/" + getYear(midpoint) , midpoint, sliderY2 + 10 );
    
    //draw selection bar 2
    fill(slider2FillColor, slider2FillColor, slider2FillColor, 125);
    rect(sliderBar2X1, sliderY1, sliderBar2X2, sliderY2);
    if( sliderBar2X1 - sliderBar1X2 > 10 ){ //if there is distance between slider bars draw normally
      rect( sliderBar2X1-15, sliderY2, sliderBar2X2+15, sliderY2+15);
      fill(255);
      textSize(9);
      textAlign(CENTER);
      midpoint = (sliderBar2X1 + sliderBar2X2)/2;
      text( getMonth( midpoint ) + "/" + getYear(midpoint) , midpoint, sliderY2 + 10 );
    } else{//else draw stacked
      rect( sliderBar2X1-15, sliderY2+15, sliderBar2X2+15, sliderY2+30);
      fill(255);
      textSize(9);
      textAlign(CENTER);
      midpoint = (sliderBar2X1 + sliderBar2X2)/2;
      text( getMonth( midpoint ) + "/" + getYear(midpoint) , midpoint, sliderY2 + 25 );
    } 
    //hovering
    textSize(10);
    fill(255);
    textAlign(RIGHT);
    if( mouseX > sliderBar1X1 && mouseX < sliderBar1X2 && mouseY > sliderY1 && mouseY < sliderY2){
      text("click and drag", mouseX-5, mouseY);
    }
    else if( mouseX > sliderBar2X1 && mouseX < sliderBar2X2 && mouseY > sliderY1 && mouseY < sliderY2){
      text("click and drag", mouseX-5, mouseY);
    }
    else if( mouseX > sliderX1 && mouseX < sliderX2 && mouseY > sliderY1 && mouseY < sliderY2){
      stroke(255);
      line(mouseX, sliderY1, mouseX, sliderY2);
      textSize(9);
      textAlign(LEFT);
      text(getMonth(mouseX) + "/" + getYear(mouseX), mouseX+2, sliderY1+8);
    }
    
    if( drawLabels ){
      drawLabels2();
    }
   
    rectMode(CORNER);//to make map program happy
  }
  
  void setSlider1Position(float newPosition){
    if( newPosition < sliderX1){//CHANGE sliderX1 + sliderBarSize/2 ){ // past left edge of slider area
      newPosition = sliderX1;//CHANGE sliderX1 + sliderBarSize/2;
    }
    float sliderBar2Mid = (sliderBar2X2 + sliderBar2X1)/2;
    if( newPosition - sliderBar2Mid > 0 ){//if past sliderBar2
      newPosition = sliderBar2Mid;//sliderBar2X1 - sliderBarSize/2;
    }
    sliderBar1X1 = newPosition - sliderBarSize/2;
    sliderBar1X2 = newPosition + sliderBarSize/2;
  }
  
  float getSlider1Position(){
    return sliderBar1X1 + sliderBarSize/2;
  }
   

  void setSlider2Position(float newPosition){
    if( newPosition > sliderX2){// CHANGE sliderX2 - sliderBarSize/2 ){ // past left edge of slider area
      newPosition = sliderX2; //CHANGE - sliderBarSize/2;
    }
    float sliderBar1Mid = (sliderBar1X1 + sliderBar1X2)/2;
    if( sliderBar1Mid - newPosition > 0){//if past sliderBar1
      newPosition = sliderBar1Mid;
    }
    sliderBar2X1 = newPosition - sliderBarSize/2;
    sliderBar2X2 = newPosition + sliderBarSize/2;
  }

  float getSlider2Position(){
    return sliderBar2X1 + sliderBarSize/2;
  }
  
  void mouseOverSlider1(){
    slider1FillColor = 128; 
  }

  void mouseOverSlider2(){
    slider2FillColor = 128;
  }
  
  float getSliderSize(){
    return sliderBarSize;
  }
 
  
  void drawLabels2(){
    fill(255);
    stroke(255);
    textAlign(CENTER, CENTER);
    for(float i = sliderMin*12; i <= sliderMax*12 + 5; i++){ //CHANGE '+ 5'
      float x = map(i, sliderMin*12, sliderMax*12 + 5, sliderX1, sliderX2); //CHANGE '+ 5'
      float y1, y2;
      if( i % (sliderMajorInterval*12) == 0 ){
        y1 = sliderY1 + (sliderY2 - sliderY1)*.35;
        y2 = sliderY2 - (sliderY2 - sliderY1)*.35;
        textSize(10);
        text((int) i/12, x, y2+7);
      }
      else if( i % (sliderMinorInterval*12) == 0 ){
        y1 = sliderY1 + (sliderY2 - sliderY1)*.45;
        y2 = sliderY2 - (sliderY2 - sliderY1)*.45;
        textSize(8);
        text((int) i/(12) , x, y2+7);
      }else{
        y1 = sliderY1 + (sliderY2 - sliderY1)*.48;
        y2 = sliderY2 - (sliderY2 - sliderY1)*.48;
      }
      line(x, y1, x, y2);
      
    }//end for loop
    
  }//end drawLabels
    
  void drawLabels(){
    fill(255);
    stroke(255);
    textAlign(CENTER, CENTER);
    for(float i = sliderMin; i <= sliderMax; i+= sliderMinorInterval){
      float x = map(i, sliderMin, sliderMax, sliderX1, sliderX2);
      float y1, y2;
      if( i % sliderMajorInterval == 0){
        y1 = sliderY1 + (sliderY2 - sliderY1)*.35;
        y2 = sliderY2 - (sliderY2 - sliderY1)*.35;
        textSize(10);
        text((int) i, x, y2+7);
      }
      else{
        y1 = sliderY1 + (sliderY2 - sliderY1)*.45;
        y2 = sliderY2 - (sliderY2 - sliderY1)*.45;
        textSize(8);
        text((int) i, x, y2+7);
      }
      line(x, y1, x, y2);
    }//end for loop
    
  }//end drawLabels


  int getMonth(float xCoord){
    float prevX = map(sliderMin*12, sliderMin*12, sliderMax*12+5, sliderX1, sliderX2);//CHANGE '+ 5'
    int prevMonth = (int) (sliderMin*12) % 12;
    for(float i = sliderMin*12; i <= sliderMax*12+5; i++){//CHANGE '+ 5'
      float x = map(i, sliderMin*12, sliderMax*12+5, sliderX1, sliderX2);//CHANGE '+ 5'
      if( abs(x - xCoord) < abs(prevX - xCoord) ){//if this coord closer than prev
        prevX = x;
        prevMonth = (int) i % 12;
      }
    }
    return prevMonth + 1;
  }
  
  int getYear(float xCoord){
    float prevX = map(sliderMin*12, sliderMin*12, sliderMax*12+5, sliderX1, sliderX2);//CHANGE '+ 5'
    int prevYear = (int) (sliderMin*12) / 12;
    for(float i = sliderMin*12; i <= sliderMax*12+5; i++){//CHANGE '+ 5'
      float x = map(i, sliderMin*12, sliderMax*12+5, sliderX1, sliderX2);//CHANGE '+ 5'
      if( abs(x - xCoord) < abs(prevX - xCoord) ){//if this coord closer than prev
        prevX = x;
        prevYear = (int) i / 12;
      }
    }
    return prevYear; 
  }
  
}// end class   



